<?php
	$pagination_url = Router::getParam('pass');
	
	$this->loadHelper('Lil.LilDate');
	$post_index = array(
		'title_for_layout' => __d('lil_blogs', 'Posts index', true),
		'scripts_for_layout' => array(
			'css' => array(
				'admin' => array(
					'path'    => '/lil_blogs/css/lil_blogs_admin',
					'rel'     => null,
					'options' => array('inline' => false)
				)
			),
			'js' => array(
				// 'name' => array('url' => $script_url, 'options' => $options_array)
			)
		),
		'menu' => array(
			'add' => array(
				'title' => __d('lil_blogs', 'Add'),
				'visible' => true,
				'url'   => array(
					'admin'      => true,
					'plugin'     => 'lil_blogs',
					'controller' => 'posts',
					'action'     => 'add',
					'blog' => empty($blog) ? null : $blog['Area']['slug']
				)
			)
		),
		'title' =>
			'<div class="head">'.
				'<h1>' . 
				(!empty($admin_area)
					?
					sprintf(
						__d('lil_blogs', 'Posts for "%s"'),
						$this->Html->clean($admin_area['Area']['name'])
					)
					:
					__d('lil_blogs', 'Posts index')
				) .
				'</h1>'.
			'</div>',
		'actions' => array(
			'pre' => '<div>',
			'post' => '</div>',
			'lines' => array(
			)
		),
		'table' => array(
			'pre' => '<div class="index">' . PHP_EOL,
			'post' => '</div>',
			'element' => array(
				'parameters' => array(
					'cellspacing' => 0,
					'cellpadding' => 0,
					'width'       => '100%',
					'id'          => 'AdminPostsIndex'
				),
			)
		)
	);
	
	$post_index['table']['element']['head'] = array(
		'parameters' => array(),
		'rows' => array(
			0 => array(
				'parameters' => array(),
				'columns' => array(
					'id' => array(
						'parameters' => array('class' => 'center'),
						'html' => $this->Paginator->sort(
							'id',
							__d('lil_blogs', 'ID'),
							array('url' => $pagination_url)
						),
					),
					'title' => array(
						'parameters' => array('class' => 'left'),
						'html' => $this->Paginator->sort(
							'title',
							__d('lil_blogs', 'Title', true),
							array('url' => $pagination_url)
						),
					),
					'author' => !Configure::read('LilBlogs.authorAssociation') ? null : array(
						'parameters' => array('class' => 'left'),
						'html' => $this->Paginator->sort(
							'author_id',
							__d('lil_blogs', 'Author'),
							array('url' => $pagination_url)
						),
					),
					'status' => array(
						'parameters' => array('class' => 'center'),
						'html' => $this->Paginator->sort(
							'status',
							__d('lil_blogs', 'Status'),
							array('url' => $pagination_url)
						),
					),
					'date' => array(
						'parameters' => array('class' => 'center'),
						'html' => $this->Paginator->sort(
							'created',
							__d('lil_blogs', 'Date'),
							array('url' => $pagination_url)
						),
					),
					'edit' => array(
						'parameters' => array(),
						'html' => '&nbsp;',
					),
					'delete' => array(
						'parameters' => array(),
						'html' => '&nbsp;',
					),
					'comments' => (Configure::read('LilBlogs.noComments')) ? null : array(
						'parameters' => array(),
						'html' => '&nbsp;',
					),
				)
			)
		)
	);
	
	// table body values
	$post_index['table']['element']['body']['rows'] = array();

	$i = 0;
	foreach($data as $item) {
		$post_index['table']['element']['body']['rows'][$i] = array(
			'post' => $item,
			'parameters' => ($i++%2==0) ? array() : array('class' => 'altrow'),
			'columns' => array(
				'id' => array(
					'parameters' => array('class' => 'center'),
					'html' => $item['Post']['id']
				),
				'title' => array(
					'parameters' => ($item['Post']['status'] == 0) ? array('class' => 'light') : array(),
					'html' => (($item['Post']['status'] == 0) ?
						('<div class="big">' . $this->Html->clean($item['Post']['title']) . '</div>') :
						('<div>' . $this->Html->link($item['Post']['title'], array(
							'admin'      => false, 
							'controller' => 'posts', 
							'action'     => 'view', 
							'blogid'     => $item['Post'][Configure::read('LilBlogs.postBlogField')], 
							'post'       => $item['Post']['slug']
						), array('class' => 'big')) . '</div>')).
						$this->Html->clean($item['Post']['slug'])
				),
				'author' => !Configure::read('LilBlogs.authorAssociation') ? null : array(
					'parameters' => array('class' => 'small'),
					'html' => $this->Html->clean($item['Author'][Configure::read('Lil.userDisplayField')])
				),
				'status' => array(
					'parameters' => array('class' => 'small center'),
					'html' => ($item['Post']['status'] == 0) ? __d('lil_blogs', 'Draft') : __d('lil_blogs', 'Live')
				),
				'date' => array(
					'parameters' => array('class' => 'nowrap center small' . (($item['Post']['status']==0) ? ' light' : '')),
					'html' => $this->LilDate->format($item['Post']['created'])
				),
				
				'edit' => array(
					'parameters' => array('class' => 'center'),
					'html' => $this->Html->link(
						$this->Html->image(
							'/lil/img/edit.gif', 
							array('alt' => __d('lil_blogs', 'Edit'))
						),
						array(
							'admin'  => true, 
							'action' => 'edit',
							$item['Post']['id']
						),
						array(
							'title' => __d('lil_blogs', 'Edit'),
							'escape' => false
						)
					)
				),
				'delete' => array(
					'parameters' => array('class' => 'center'),
					'html' => $this->Html->link(
						$this->Html->image('/lil/img/delete.gif',
							array('alt'=>__d('lil_blogs', 'Delete'))
						),
						array(
							'admin'  => true, 
							'action' => 'delete',
							$item['Post']['id']
						),
						array(
							'title' => __d('lil_blogs', 'Delete'),
							'confirm' => sprintf(__d('lil_blogs', 'Are you sure you want to delete \'%s\'?'), $item['Post']['title']),
							'escape' => false
						)
					)
				),
				'comments' => (Configure::read('LilBlogs.noComments')) ? null : array(
					'parameters' => array('class' => 'center'),
					'html' => $this->Html->link($item['Post']['no_comments'],
						array(
							'admin'      => true,
							'controller' => 'comments', 
							'action'     => 'index',
							'post_id'    => $item['Post']['id']
						), 
						array(
							'title' => __d('lil_blogs', 'Comments'), 
							'class' => 'PostsIndexCommentCount'
						),
						null, false)
				),
			)
		);
			

	}
	
	$post_index['table']['element']['foot'] = array(
		'parameters' => array(),
		'rows' => array(
			0 => array(
				'parameters' => array(),
				'columns' => array(
					'checkbox' => array(
						'parameters' => array('class' => 'center'),
						'html' => '<input type="checkbox" id="SelectAll2" onclick="DoSelectAll(this);" />'
					),
					'paginator' => array(
						'parameters' => array('colspan' => '7', 'class' => 'paging'),
						'html'       => $this->Paginator->numbers()
					),
				)
			)
		)
	);
	
	///////////////////////////////////////////////////////////////////////////////////////////////
	// call plugin handlers
	$post_index = $this->callPluginHandlers('lil_blogs_admin_index_posts', $post_index);
	
	$this->Lil->index($post_index);